package cc.web.vaaking.services;

import cc.web.vaaking.common.DB.SuperSessionFactory;
import cc.web.vaaking.entity.SysUser;
import cc.web.vaaking.mapper.MiscMapper;
import cc.web.vaaking.mapper.SysUserMapper;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class SysUserService {

    public SysUser selectByUsername(String username) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
            return sysUserMapper.selectByUsername(username);
        }
    }

    public int deleteByPrimaryKey(Integer id) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession(true)) {
            SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
            return sysUserMapper.deleteByPrimaryKey(id);
        }
    }

    public int insert(SysUser record) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession(true)) {
            String password = record.getPassword();
            String email = record.getEmail();
            String username = record.getUsername();
            if (StrUtil.isBlank(username))
                return 0;
            if (StrUtil.isBlank(password) && StrUtil.isBlank(email)) {
                return 0;
            }
            if (StrUtil.isBlank(password))
                record.setPassword(RandomUtil.randomString(6));
            SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
            MiscMapper miscMapper = session.getMapper(MiscMapper.class);
            sysUserMapper.insert(record);
            miscMapper.addUserRole(record.getId(), 3);
            if (!StrUtil.isBlank(email)) {
                MailUtil.send(email, "新用户验证", StrUtil.format("你好，你的用户名为{}，密码为 {} ",record.getUsername(), record.getPassword()), false);
            }
            return 1;
        }
    }

    public int insertSelective(SysUser record) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
            return sysUserMapper.insertSelective(record);
        }
    }

    public SysUser selectByPrimaryKey(Integer id) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
            return sysUserMapper.selectByPrimaryKey(id);
        }
    }

    public int updateByPrimaryKeySelective(SysUser record) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession(true)) {
            SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
            return sysUserMapper.updateByPrimaryKeySelective(record);
        }
    }

    public int updateByPrimaryKey(SysUser record) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
            return sysUserMapper.updateByPrimaryKey(record);
        }
    }

    public List<SysUser> selectAllSysUser() {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
            return sysUserMapper.selectAllSysUser();
        }
    }
}
